/**
 * check the input company name whether duplicated. 
 * @return
 */
function checkDuplicateCompanyName(){
	var url = "ajaxcheckcompanyname.action";
	var companyId = $("#company_companyId").val();
	var companyName = $("#company_companyName").val();
	$.post(url, {"companyId":companyId,"companyName":trim(companyName)}, function(data){
		if(!isTimeout(data,false)){
			if(data != 0){	
				alert("单位名称已经存在,请修改单位名称");
				resetButton();
			}else{
				$("#update_company_form").submit();
			}
		}
		}
	);	
}

/**
 * when add a company successfully, refresh the company list
 * @return
 */
function refreshCompany(){
	showSuccess();
	$("#company_list_form").submit();
}

/**
 * pop up the delete dialogue frame
 * @param companyId
 * @return
 */
function toDeleteCompany(companyId){
	$("#delete_company_id").val(companyId);
	showDelete();
}
/**
 * Delete company
 * @return
 */
function deleteCompany() {
	var deleteForm = document.getElementById("company_delete_form");
	deleteForm.submit();
}

/**
 * This function used for pagination
 * @param pageNo
 * @return
 */
function findElementList(pageNo){
	document.getElementById("current_page").value = pageNo;	
	document.getElementById("company_list_form").submit();

}

/**
 * send an ajax request to add a new company 
 * @param parentCompanyId
 * @param level
 * @return
 */
function sendAddCompanyRequest(parentCompanyId, level){
	var url = "ajaxprovincialcompanyadd.action";
	var name = $("#company_companyName").val();
	var telephone = $("#company_telephone").val();
	var fax = $("#company_fax").val();
	var email = $("#company_email").val();
	var address = $("#company_address").val();
	var postCode = $("#company_postCode").val();;
	var remark = $("#company_remark").val();
	if(checkCompanyInfo(name,telephone,fax,email,address,postCode,remark)){
		var params = {	"company.parentCompanyId":parentCompanyId,
						"company.companyLevel":level,
						"company.companyName":name,
						"company.telephone":telephone,
						"company.fax":fax,
						"company.email":email,
						"company.address":address,
						"company.postCode":postCode,
						"company.remark":remark}; 
		$.post("ajaxcheckcompanyname.action", 
				{"companyName":trim(name)}, 
				function(data){
					if(!isTimeout(data,false)){
						if(data != 0){	
							alert("单位名称已经存在,请修改单位名称");
							resetButton();
						}else{
							$.post(url, params, function(data){
								showAdd();
								$("#new_company_id_p").html(data);
								showSuccess();
								
							});	
						}
					}
				}
				);
	}else{
		resetButton();
	}
}

function disableMe(button){
	button.disabled = "true";
}

function resetButton(){
	$("#ok_button").attr("disabled",false);
}
/**
 * discard the blank at head and tail
 * @param string
 * @return
 */
function trim(string){
	return string.replace(/(^\s*)|(\s+$)/g,"");
}
/**
 * check the company's information whether valid 
 * @param name
 * @param telephone
 * @param fax
 * @param email
 * @param address
 * @param postCode
 * @param remark
 * @return
 */
function checkCompanyInfo(name,telephone,fax,email,address,postCode,remark){
	return checkName(trim(name)) && 
			checkTel(trim(telephone)) &&
			checkFax(trim(fax)) &&
			checkEmail(trim(email)) &&
			checkAddress(trim(address)) &&
			checkPostCode(trim(postCode)) &&
			checkRemark(trim(remark));
}
/**
 * check name
 * @param name
 * @return
 */
function checkName(name){
	if(name == ''){
		alert("单位名称不能为空");
		return false;
	}
	if(name.length > 50){
		alert("单位名称长度不能超过50个字符");
		return false;
	}
	return specialChar(name);
}
/**
 * check telephone
 * @param telephone
 * @return
 */
function checkTel(telephone){
	if(telephone.length > 50){
		alert("电话号码长度不能超过50个字符");
		return false;
	}

	var patrn = /[^\+|\-|\(|\)|\d| ]/;
	if (telephone.length > 0 && telephone.match(patrn)) {
	    alert("电话号码格式不正确");
	    return false;
	} 
	return true;
}

/**
 * check fax
 * @param fax
 * @return
 */
function checkFax(fax){
	if(fax.length > 50){
		alert("传真号码长度不能超过50个字符");
		return false;
	}
	var patrn = /[^\+|\-|\(|\)|\d| ]/;  
	if (fax.length > 0 && fax.match(patrn)) {
	    alert("传真号码格式不正确");
	    return false;
	} 
	return true;
}

/**
 * check email
 * @param email
 * @return
 */
function checkEmail(email){
	if(email.length > 50){
		alert("电子邮箱长度不能超过50个字符");
		return false;
	}
	var patrn = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
	if (email.length > 0 && !patrn.exec(email)) {
	    alert("电子邮箱格式不正确");
	    return false;
	} 
	return true;	
}

/**
 * check address
 * @param address
 * @return
 */
function checkAddress(address){
	if(address.length > 50){
		alert("地址长度不能超过50个字符");
		return false;
	}
	return specialChar(address);
}

/**
 * check post code
 * @param postCode
 * @return
 */
function checkPostCode(postCode){
	var patrn=/^[0-9]{1,6}$/; 
	if (postCode.length > 0 && !patrn.exec(postCode)) {
	    alert("邮编只能为6位以内的数字");
	    return false;
	} 
	return true;
}

/**
 * check remark
 * @param remark
 * @return
 */
function checkRemark(remark){
	if(remark.length > 255){
		alert("备注信息长度不能超过255个字符");
		return false;
	}
	return specialChar(remark);
}

function specialChar(str){
	var patrn=/[\$#{}\[\]%\^&*()]/; 
	var result;
	if((result = str.match(patrn))!= null){
		alert("存在非法字符" + result);
		return false;
	}
	return true;
}
/**
 * select municipal companies by provincial company id
 * @return
 */
function selectMunicipalCompanyById() {
	var companyId = $("#select_provincial_company_add").val();
	var url = "ajaxfindcompanynamesbyid.action";
	$.post(url, {"companyId":companyId}, function(data){
		if(!isTimeout(data,false)){
			$("#select_municipal_company_add").html(data);
		}
		}
		);
}

/**
 * select provincial companies by company level
 * @return
 */
function selectProvincialCompanyByLevel(){
	var url = "ajaxfindcompanynamesbylevel.action";
	var level = 2;
	$.post(url, {"companyLevel":level}, function(data){
		if(!isTimeout(data,false)){
			$("#select_provincial_company_add").html(data);
		}
		}
		);
}

function selectHomeOffice(){
	var url = "ajaxfindcompanynamesbylevel.action";
	var level = 1;
	$.post(url, {"companyLevel":level}, function(data){
		if(!isTimeout(data,false)){
			$("#parent_company_id").html(data);
		}
		}
		);
}

function turnToPage(event){
	if(event.keyCode == 13) {
		var pageNum = $("#input_page_no").val();
		pageNum = trim(pageNum);
		var pattern = /^[1-9][0-9]*$/; 
		if (!pattern.exec(pageNum)) {
			alert("输入的页码不正确");
			return false;
		}
		var totalPageNum = $("#total_page_num").val();
		if(pageNum > totalPageNum){
			pageNum = totalPageNum;
		}
		$("#current_page").val(pageNum);
		$("#company_list_form").submit();
	}
}


function checkInputData(pig) {		
	var val = pig.value;
	var patrn = new RegExp("[\|~`!@#\$%\^&\*\<\>\{\}()\+\-]+");	
	if(!val) {
        return true;
	}
	if (val.match(patrn) == null) {
		return true;
	}  else {
	   pig.value=pig.value.substr(0,pig.value.length-1);
	}	
}

function checkInputRemark(tt) {		
	var val = tt.value;
	var patrn = new   RegExp("[\~`;'\"\^\*\<\>\]");
	if(!val) {
        return true;
	}
	if (val.match(patrn) == null) {
		return true;
	}  else {
	   tt.value=tt.value.substr(0,tt.value.length-1);
	}	
}

function back(action){
	var updateForm = document.getElementById("update_company_form");
	updateForm.action = action; 
	updateForm.submit();
}